package com.dnjn.happiness.survey.controller.admin;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.survey.domain.Tiku;
import com.dnjn.happiness.survey.service.ITikuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 题库题目Controller
 *
 * @author happiness
 * @date 2023-10-24
 */
@Tag(name = "admin survey tiku 题库题目", description = "题库题目")
@RestController
@RequestMapping("/admin/tiku")
public class AdminTikuController extends BaseController {
    @Autowired
    private ITikuService tikuService;

    /**
     * 查询题库题目列表
     */
    @PostMapping("/list")
    @RequiresPermissions("survey:tiku:list")
    public AjaxResult list(@RequestBody Tiku tiku, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size);
        Page<Tiku> list = tikuService.page(page, Wrappers.query(tiku).eq("deleted", "0"));
        return success(list);
    }

    /**
     * 导出题库题目列表
     */
    @Log(title = "题库题目", businessType = BusinessType.EXPORT)
    @RequiresPermissions("survey:tiku:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tiku tiku) {
        List<Tiku> list = tikuService.list(Wrappers.query(tiku));
        ExcelUtil<Tiku> util = new ExcelUtil<Tiku>(Tiku.class);
        util.exportExcel(response, list, "题库题目数据");
    }

    /**
     * 获取题库题目详细信息
     */
    @RequiresPermissions("survey:tiku:query")
    @GetMapping(value = "/get/{tikuId}")
    public AjaxResult get1(@PathVariable("tikuId") Long tikuId) {
        return success(tikuService.getById(tikuId));
    }


    /**
     * wx获取题库题目详细信息
     */
    @GetMapping(value = "/{tikuId}")
    public AjaxResult get(@PathVariable("tikuId") Long tikuId) {
        return success(tikuService.getById(tikuId));
    }

    /**
     * 新增题库题目
     */
    @Log(title = "题库题目", businessType = BusinessType.INSERT)
    @RequiresPermissions("survey:tiku:add")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Tiku tiku) {
        Assert.notNull(tiku.getIsSelectPack(), "类型不能为空");
        if ("pack".equals(tiku.getIsSelectPack())) {
            tiku
                    .setIsMultipleChoice(null)
                    .setOptionA(null)
                    .setOptionB(null)
                    .setOptionC(null)
                    .setOptionD(null)
                    .setOptionE(null)
                    .setOptionF(null)
                    .setOptionG(null)
                    .setAnswer(null);
        }
        tiku.setCreateBy(SecurityUtils.getUsername());
        return toAjax(tikuService.save(tiku));
    }

    /**
     * 修改题库题目
     */
    @Log(title = "题库题目", businessType = BusinessType.UPDATE)
    @RequiresPermissions("survey:tiku:edit")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Tiku tiku) {
        Assert.notNull(tiku.getIsSelectPack(), "类型不能为空");
//        Assert.notNull(tiku.getIsMultipleChoice(),"是否多选不能为空");
//        Assert.notNull(tiku.getAnswer(),"答案不能为空");
        if ("pack".equals(tiku.getIsSelectPack())) {
            tiku.setIsMultipleChoice(null);
            return toAjax(tikuService.lambdaUpdate().eq(Tiku::getTikuId, tiku.getTikuId())
                    .set(Tiku::getTikuTagsId, tiku.getTikuTagsId())
                    .set(Tiku::getTitle, tiku.getTitle())
                    .set(Tiku::getIsSelectPack, tiku.getIsSelectPack())
                    .set(Tiku::getIsMultipleChoice, null)
                    .set(Tiku::getOptionA, null)
                    .set(Tiku::getOptionB, null)
                    .set(Tiku::getOptionC, null)
                    .set(Tiku::getOptionD, null)
                    .set(Tiku::getOptionE, null)
                    .set(Tiku::getOptionF, null)
                    .set(Tiku::getOptionG, null)
                    .set(Tiku::getAnswer, null)
                    .set(Tiku::getScoreOptionA, null)
                    .set(Tiku::getScoreOptionB, null)
                    .set(Tiku::getScoreOptionC, null)
                    .set(Tiku::getScoreOptionD, null)
                    .set(Tiku::getScoreOptionE, null)
                    .set(Tiku::getScoreOptionF, null)
                    .set(Tiku::getScoreOptionG, null)
                    .update());
        }
        return toAjax(tikuService.updateById(tiku));
    }

    /**
     * 删除题库题目
     */
    @Log(title = "题库题目", businessType = BusinessType.DELETE)
    @RequiresPermissions("survey:tiku:remove")
    @DeleteMapping("/{tikuIds}")
    public AjaxResult remove(@PathVariable Long[] tikuIds) {
        for (Long tikuId : tikuIds) {
            tikuService.lambdaUpdate().eq(Tiku::getTikuId, tikuId).set(Tiku::getDeleted, "1").update();
        }
        return success();
    }

    @Operation(summary = "小程序-查询题库里边题目列表-根据tikuTagsId这个字段")
    @PostMapping("/listQueryTikuList")
    @RequiresPermissions("survey:tiku:list")
    public AjaxResult listQueryX(@RequestBody Tiku domain, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page<Tiku> list = tikuService.selectPageX(domain, Page.of(current, size).addOrder(OrderItem.desc("tiku_id")));
        return success(list);

    }
}
